<?php
namespace app\admin\controller;
use app\model\Contact as ContactModel;
use think\Db;


class Contact extends Base
{
    public function __construct(){
        $model = new ContactModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '客户联系';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }


    public function changeData(){
        $id = input('id');
        if (empty($id)) { return ['status'=>0,'msg'=>'id不能为空']; }
        $fields = [
            'must'=>['status']
        ];
        $save_data = paramFilter(request()->param(),$fields);
        if (empty($save_data)) {
            return ['status'=>0,'msg'=>'无任何修改'];
        }
        return parent::changeData($id,$save_data);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'truename','type'=>'like'],
            ['key'=>'mobile','type'=>'like'],
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        $search_params = [
            [
                ['key'=>'truename','name'=>'姓名','type'=>'text'],
                ['key'=>'mobile','name'=>'手机','type'=>'text'],
                ['key'=>'status','name'=>'状态','type'=>'select1_1','params'=>$model->getStatusList()],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
        return parent::index($map);
    }
    
    public function export(){
        $model = $this->model;
        $map_params = [
            ['key'=>'truename','type'=>'like'],
            ['key'=>'mobile','type'=>'like'],
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        
        $list = $model->where($map)->order('id','desc')->select();
        $data = [];
        $title = ['ID','姓名','手机','邮箱','公司名称','联系地址','IP地址','状态','创建时间'];
        foreach ($list as $k => $vo) {
            $tmp = [
                $vo['id'],
                exportReplace($vo['truename']),
                exportReplace($vo['mobile']),
                $vo['email'],
                exportReplace($vo['company']),
                exportReplace($vo['address']),
                $vo['ip_address'],$model->getStatusCn($vo['status']),
                timeDef($vo['ctime'])
            ];
            $data[] = $tmp;
        }
        exportCsv(date('YmdHis'),$title,$data);
    }
}
